package com.data.util;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * json格式
 *
 * @author:HingLo
 * @create 2017-09-27 18:13
 **/
public class JsonUtils {
    /**
     * 将list集合，转为Map类型的数组
     *
     * @param list  数据集合
     * @param value 集合中值得对应名称
     * @return
     */

    public static Object[] JsonUtil(List<?> list, String[] downUrl, String... value) {
        Object[] result = new Object[list.size()];
        int len = list.size();
        for (int i = 0; i < len; i++) {
            Object[] o = (Object[]) list.get(i);
            int len1 = o.length;
            Map<String, Object> map = new HashMap<>();
            for (int j = 0; j < len1; j++) {
                map.put(value[j], o[j]);
            }
            if (downUrl != null && downUrl.length > 0) {
                if (map.containsKey("id")) {
                    StringBuilder stringBuilder = new StringBuilder();
                    //缩略图URL
                    map.put("IconUrl", stringBuilder.append(downUrl[0]).append(map.get("id")).toString());
                }

                if (map.containsKey("md5")) {
                    StringBuilder stringBuilder = new StringBuilder();
                    //下载URL
                    map.put("downloadUrl", stringBuilder.append(downUrl[1]).append(map.get("md5")).append("&id=").append(map.get("id")).toString());
                }
            }

            result[i] = map;
        }
        return result;
    }

    /**
     * 统一处理结果为json格式
     *
     * @param list
     * @param value
     * @param iconUrl 图片的URL
     * @return
     */
    public static Map<String, Object> detailListToMapUtils(List<?> list, String[] iconUrl, String... value) {
        Map<String, Object> map = new HashMap<>();
        if (list != null && !list.isEmpty()) {
            map.put("code", 200);
            map.put("msg", JsonUtil(list, iconUrl, value));
        } else {
            map.put("code", 400);
            map.put("msg", "error");
        }
        return map;
    }


    /**
     * 返回项目的根路径
     *
     * @param request
     * @return
     */
    public static String getBasePath(HttpServletRequest request) {
        String p = String.valueOf(request.getServerPort());
        StringBuilder basePath = null;
        if (p.equals("80")) {
            basePath = new StringBuilder(request.getScheme()).append("://").append(request.getServerName()).append(request.getContextPath());
        } else {
            basePath = new StringBuilder(request.getScheme()).append("://").append(request.getServerName()).append(":").append(p).append(request.getContextPath());

        }
        return basePath.toString();
    }

    /**
     * 解析软件详情的大图
     *
     * @param list
     * @param url
     * @return
     */

    public static Map<String, Object> detailSoftwareDetailUtils(List<?> list, String url) {
        Map<String, Object> map = new HashMap<>();
        if (list != null && !list.isEmpty()) {
            map.put("code", 200);
            map.put("msg", JsonSoftwareUtil(list, url));
        } else {
            map.put("code", 400);
            map.put("msg", "error");
        }
        return map;
    }


    /**
     * 将list集合，转为Map类型的数组
     *
     * @param list 数据集合
     * @param url  集合中值得对应名称
     * @return
     */

    public static Object[] JsonSoftwareUtil(List<?> list, String url) {
        Object[] result = new Object[list.size()];
        int len = list.size();
        for (int i = 0; i < len; i++) {
            Object[] o = (Object[]) list.get(i);
            String dUrl = url + o[0] + "&name=" + o[1];
            Map<String, String> map = new HashMap<>();
            map.put("url", dUrl);
            result[i] = map;
        }
        return result;
    }

    /**
     * 消息返回处理格式
     *
     * @param bool 是否成功
     * @param data 错误消息
     * @return
     */

    public static Map<String, Object> returnMassageUtils(boolean bool, Object data) {
        Map<String, Object> map = new HashMap<>();
        if (bool) {
            map.put("code", 200);
        } else {
            map.put("code", 400);
        }
        map.put("msg", data);
        return map;
    }

    /**
     * 消息返回处理格式
     *
     * @param data 错误消息
     * @return
     */

    public static Map<String, Object> returnMassageUtils(String[] value, List<?> data) {
        Map<String, Object> map = new HashMap<>();
        for (Object o : data) {
            Object ox[] = (Object[]) o;
            int x = (int) ox[0];
            map.put(value[x - 1], ox[1]);
        }
        return map;
    }

}
